package com.tensquare.recruit.service;

import com.tensquare.recruit.dao.EnterpriseDao;
import com.tensquare.recruit.pojo.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:zimmor
 * @Date: 2021/3/1 8:58
 * @Description
 * @version: 1.0.0
 */
@Service
public class EnterpriseService {
    @Autowired
    private EnterpriseDao enterpriseDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询热门企业
     * @param ishot
     * @return
     */
    public List<Enterprise> ishot(String ishot) {
        return enterpriseDao.findByIshot(ishot);
    }

    /**
     * 分页查询
     * @param enterprise
     * @param page
     * @param size
     * @return
     */
    public Page<Enterprise> findSearch(Enterprise enterprise, int page, int size) {
        Pageable pageable = PageRequest.of(page-1,size);
        Specification<Enterprise> specification = new Specification<Enterprise>() {
            @Override
            public Predicate toPredicate(Root<Enterprise> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList();
                if(enterprise.getName()!=null && !"".equals(enterprise.getName())){
                    Predicate name = criteriaBuilder.like(root.get("name").as(String.class), "%" + enterprise.getName() + "%");
                    list.add(name);
                }
                if(enterprise.getIshot()!=null && !"".equals(enterprise.getIshot())){
                    Predicate ishot = criteriaBuilder.equal(root.get("ishot").as(String.class), enterprise.getIshot());
                    list.add(ishot);
                }
                Predicate[] predicates = new Predicate[list.size()];
                list.toArray(predicates);
                return criteriaBuilder.and(predicates);
            }
        };
        return enterpriseDao.findAll(specification, pageable);
    }

    /**
     * 条件查询
     * @param enterprise
     * @return
     */
    public List<Enterprise> findSearch(Enterprise enterprise) {
        return enterpriseDao.findAll(new Specification<Enterprise>() {
            @Override
            public Predicate toPredicate(Root<Enterprise> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                if(enterprise.getName()!=null && !"".equals(enterprise.getIshot())){
                    Predicate name = criteriaBuilder.like(root.get("name").as(String.class), "%" + enterprise.getName() + "%");
                    list.add(name);
                }
                if(enterprise.getIshot()!=null && !"".equals(enterprise.getIshot())){
                    Predicate ishot = criteriaBuilder.equal(root.get("ishot").as(String.class), enterprise.getIshot());
                    list.add(ishot);
                }
                Predicate[] predicates = new Predicate[list.size()];
                list.toArray(predicates);
                return criteriaBuilder.and(predicates);
            }
        });
    }

    /**
     * 根据id查询企业
     * @param id
     * @return
     */
    public Enterprise findById(String id) {
        return enterpriseDao.findById(id).get();
    }

    /**
     * 新增数据
     * @param enterprise
     */
    public void add(Enterprise enterprise) {
        enterprise.setId(idWorker.nextId()+"");
        enterpriseDao.save(enterprise);
    }

    /**
     * 修改数据
     * @param enterprise
     */
    public void update(Enterprise enterprise) {
        enterpriseDao.save(enterprise);
    }

    /**
     * 删除数据
     * @param id
     */
    public void delete(String id) {
        enterpriseDao.deleteById(id);
    }
}
